import tkinter as tk
from tkinter import ttk
from base_gui import BaseWellGUI

class InputWellGUI(BaseWellGUI):
    def __init__(self, root, calculator):
        super().__init__(root)
        self.calculator = calculator
        
        # 创建基本参数输入区
        self.create_basic_inputs()
        
        # 创建分段参数输入区
        self.create_segment_inputs()
        
        # 创建分节参数输入区
        self.create_section_inputs()
        
        # 设置默认值
        self.set_default_values()
    
    def create_basic_inputs(self):
        # 基本参数输入 - 使用网格布局使其更紧凑
        basic_frame = ttk.Frame(self.basic_frame)
        basic_frame.pack(fill=tk.X, padx=5, pady=5)
        
        labels = [
            "井深度 (m):", "内径 (m):", 
            "井顶面标高 (m):", "设计地面标高 (m):", 
            "混凝土自重 (kN/m³):"
        ]
        self.basic_entries = []
        
        for i, label in enumerate(labels):
            row = i // 1  # 单列布局
            col = i % 1
            lbl = ttk.Label(basic_frame, text=label, width=18)
            lbl.grid(row=row, column=col*2, padx=3, pady=3, sticky=tk.W)
            
            entry = ttk.Entry(basic_frame, width=12)
            entry.grid(row=row, column=col*2+1, padx=3, pady=3)
            self.basic_entries.append(entry)
    
    def create_segment_inputs(self):
        # 分段参数输入 - 可动态调整数量
        segment_frame = ttk.Frame(self.segment_frame)
        segment_frame.pack(fill=tk.X, padx=5, pady=5)
        
        # 添加分段数量选择
        ttk.Label(segment_frame, text="分段数量:").grid(row=0, column=0, padx=5, pady=5)
        self.segment_count = tk.IntVar(value=4)
        segment_spinbox = ttk.Spinbox(segment_frame, from_=1, to=10, textvariable=self.segment_count, width=5)
        segment_spinbox.grid(row=0, column=1, padx=5, pady=5)
        # 使用trace代替bind来监控值变化
        self.segment_count.trace_add("write", lambda *args: self.update_segment_inputs())
        
        # 创建表头
        headers = ["分段", "长度 (m)", "壁厚 (m)"]
        for col, header in enumerate(headers):
            lbl = ttk.Label(segment_frame, text=header, font=('SimHei', 9, 'bold'))
            lbl.grid(row=1, column=col, padx=5, pady=2)
        
        # 存储输入框的框架
        self.segment_input_frame = ttk.Frame(segment_frame)
        self.segment_input_frame.grid(row=2, column=0, columnspan=3, sticky=tk.W)
        
        # 初始化分段输入框
        self.segment_entries = []
        self.update_segment_inputs()
    
    def create_section_inputs(self):
        # 分节参数输入 - 可动态调整数量
        section_frame = ttk.Frame(self.section_frame)
        section_frame.pack(fill=tk.X, padx=5, pady=5)
        
        # 添加分节数量选择
        ttk.Label(section_frame, text="分节数量:").grid(row=0, column=0, padx=5, pady=5)
        self.section_count = tk.IntVar(value=6)
        section_spinbox = ttk.Spinbox(section_frame, from_=1, to=10, textvariable=self.section_count, width=5)
        section_spinbox.grid(row=0, column=1, padx=5, pady=5)
        # 使用trace代替bind来监控值变化
        self.section_count.trace_add("write", lambda *args: self.update_section_inputs())
        
        # 创建表头
        headers = ["分节", "长度 (m)"]
        for col, header in enumerate(headers):
            lbl = ttk.Label(section_frame, text=header, font=('SimHei', 9, 'bold'))
            lbl.grid(row=1, column=col, padx=5, pady=2)
        
        # 存储输入框的框架
        self.section_input_frame = ttk.Frame(section_frame)
        self.section_input_frame.grid(row=2, column=0, columnspan=2, sticky=tk.W)
        
        # 初始化分节输入框
        self.section_entries = []
        self.update_section_inputs()
    
    def update_segment_inputs(self, event=None):
        # 保存当前输入值
        current_values = []
        for length_entry, thickness_entry in self.segment_entries:
            current_values.append((length_entry.get(), thickness_entry.get()))
        
        # 清空现有输入框
        for widget in self.segment_input_frame.winfo_children():
            widget.destroy()
        
        self.segment_entries = []
        count = self.segment_count.get()
        
        # 创建新的输入框
        for i in range(count):
            lbl = ttk.Label(self.segment_input_frame, text=f"第{i+1}段:")
            lbl.grid(row=i, column=0, padx=5, pady=2)
            
            length_entry = ttk.Entry(self.segment_input_frame, width=10)
            length_entry.grid(row=i, column=1, padx=5, pady=2)
            
            thickness_entry = ttk.Entry(self.segment_input_frame, width=10)
            thickness_entry.grid(row=i, column=2, padx=5, pady=2)
            
            # 恢复保存的值或设置默认值
            if i < len(current_values):
                length_val, thickness_val = current_values[i]
                length_entry.insert(0, length_val)
                thickness_entry.insert(0, thickness_val)
            else:
                length_entry.insert(0, "0")
                thickness_entry.insert(0, "0.5")
            
            self.segment_entries.append((length_entry, thickness_entry))
        
        # 如果是从clear_data调用的，不设置默认值
        if event is not None:
            self.set_segment_default_values()
    
    def update_section_inputs(self, event=None):
        # 保存当前输入值
        current_values = [entry.get() for entry in self.section_entries]
        
        # 清空现有输入框
        for widget in self.section_input_frame.winfo_children():
            widget.destroy()
        
        self.section_entries = []
        count = self.section_count.get()
        
        # 创建新的输入框
        for i in range(count):
            lbl = ttk.Label(self.section_input_frame, text=f"第{i+1}节:")
            lbl.grid(row=i, column=0, padx=5, pady=2)
            
            length_entry = ttk.Entry(self.section_input_frame, width=10)
            length_entry.grid(row=i, column=1, padx=5, pady=2)
            
            # 恢复保存的值或设置默认值
            if i < len(current_values):
                length_entry.insert(0, current_values[i])
            else:
                length_entry.insert(0, "0")
            
            self.section_entries.append(length_entry)
        
        # 如果是从clear_data调用的，不设置默认值
        if event is not None:
            self.set_section_default_values()